<template>
    <div>
        <div class="table-title">
            库存添加
        </div>
        <div class="table-padding">
            <div style="margin: 20px;"></div>
            <el-form :model="modelForm" ref="modelForm" label-width="100px" class="demo-ruleForm" style="width:500px">
                <el-form-item label="物品id" prop="materialId" :rules="[]">
                    <el-input type="text" v-model.number="modelForm.materialId" placeholder="请输入物品id" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="仓库id" prop="warehouseId" :rules="[]">
                    <el-input type="text" v-model.number="modelForm.warehouseId" placeholder="请输入仓库id" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="库存数量" prop="count" :rules="[]">
                    <el-input type="text" v-model.number="modelForm.count" placeholder="请输入库存数量" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="成本均价" prop="cost" :rules="[]">
                    <el-input type="text" v-model.number="modelForm.cost" placeholder="请输入成本均价" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="库存金额" prop="amount" :rules="[]">
                    <el-input type="text" v-model.number="modelForm.amount" placeholder="请输入库存金额" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('modelForm')">提交</el-button>
                    <el-button @click="resetForm('modelForm')">重置</el-button>
                    <el-button  type="success" @click="goBack()">返回</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
    //注意路径
    export default{
        data(){
            return {
                 modelForm: {
                 }
            };
        },
        created(){
            window.this = this;
            this.initData();
        },
        methods: {
            initData(){
                this.$axios.get("store/stocks/"+this.$route.query.id).then((data)=>{
                    if(data.status==200){
                        this.modelForm=data.content;
                        if(data.content && data.content.state){
                            this.modelForm.state=data.content.state.key;
                        }
                    }else{
                        this.$message.error(data.message);
                    }
                })
            },
            goBack() {
                this.router.go(-1);
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.modelForm._method="PUT";
                        this.$axios.put("store/stocks/"+this.$route.query.id+"/update",this.modelForm).then((data)=>{
                            if(data.status==200){
                                this.$message({
                                    message:data.message,
                                    type: 'success'
                                });
                                this.router.go(-1);
                            }else{
                                this.$message.error(data.message);
                            }
                        });
                    } else {
                        this.$message.error("请检查输入项");
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>
<style scoped>

</style>